/**
 * Created by caosh on 2018/1/19 0019.
 */
var Order = new Vue({
    el: '#my-order',
    data: {
        tip: '我的订单',
        selected: '1',
        orders: []
    },
    computed: {
    },
    methods: {
        goBack: function () {
            window.history.back(-1);
        },
        getOrders: function () {
            var _this = this;
            // 通过后台 获得订单
            /**
             $.post(url, , function(result){
                _this.orders = result;
             }, 'json');
             */
            var result = [{
                date: '2017-11-13 10:10:05',
                orderNO: '20171113192901',
                orderState: '待分拣',
                total: '1000.00',
                orderPay: '微信支付',
                payState: '已支付',
                products: [
                    {
                        name: '[日月鲜]一级牛肉饼干',
                        num: '5',
                        unitPrice: '',
                        total: '500.00'
                    },
                    {
                        name: '[日月鲜]一级牛肉饼干',
                        num: '5',
                        unitPrice: '',
                        total: '500.00'
                    }
                ]
            }, {
                date: '2017-11-13 10:10:05',
                orderNO: '20171113192900',
                orderState: '待付款',
                total: '1000.00',
                orderPay: '微信支付',
                payState: '未支付',
                products: [
                    {
                        name: '[日月鲜]一级牛肉饼干',
                        num: '5',
                        unitPrice: '',
                        total: '500.00'
                    },
                    {
                        name: '[日月鲜]一级牛肉饼干',
                        num: '5',
                        unitPrice: '',
                        total: '500.00'
                    }
                ]
            }, {
                date: '2017-11-13 10:10:05',
                orderNO: '20171113192899',
                orderState: '已收货',
                total: '500.00',
                orderPay: '微信支付',
                payState: '已支付',
                products: [
                    {
                        name: '[日月鲜]一级牛肉饼干',
                        num: '5',
                        unitPrice: '',
                        total: '500.00'
                    }
                ]
            }, {
                date: '2017-11-13 10:10:05',
                orderNO: '20171113192898',
                orderState: '已取消',
                total: '500.00',
                orderPay: '微信支付',
                payState: '已支付',
                products: [
                    {
                        name: '[日月鲜]一级牛肉饼干',
                        num: '5',
                        unitPrice: '',
                        total: '500.00'
                    }
                ]
            }, {
                date: '2017-11-13 10:10:05',
                orderNO: '20171113192897',
                orderState: '待收货',
                total: '500.00',
                orderPay: '微信支付',
                payState: '已支付',
                products: [
                    {
                        name: '[日月鲜]一级牛肉饼干',
                        num: '5',
                        unitPrice: '',
                        total: '500.00'
                    }
                ]
            }];

            _this.orders = result;
        },
        // tab切换时 更新页面数据
        getOrdersByType: function (selected) {
            return this.orders.filter(function(o) {
                if (selected == '2') {
                    return o.orderState == '待收货' || o.orderState == '待分拣';
                } else if (selected == '3') {
                    return o.orderState == '待付款';
                } else if (selected == '4') {
                    return o.orderState == '已收货';
                } else if (selected == '5') {
                    return o.orderState == '已取消';
                }
            });
        },
        // 取消订单
        cancelOrder: function (order) {
            // 请求后台 取消订单
            /**
             $.post(url, order, function(result){
                if (result.success) {
                    order.orderState = '已取消';
                }
             }, 'json');
             */

            var result = {success: true};
            if (result.success) {
                order.orderState = '已取消';
            }
        },
        // 再来一单
        tooOrder: function (order) {
            var newOrder = this.deepCopy(order);
            var now = new Date();
            newOrder.orderState = '待付款';
            newOrder.payState = '未支付';
            newOrder.orderNO = new Date().getTime(); // 订单号需更换为其他算法
            newOrder.date = DateFormat.format(now, 'yyyy-MM-dd hh:mm:ss');
            var _this = this;

            // 请求后台 添加订单信息
            /**
             $.post(url, newOrder, function(result){
                if (result) {
                    _this.orders.unshift(newOrder);
                    _this.selected = '3'; // 切换到待付款页面
                }
             }, 'json');
             */
            var result = {success: true};
            if (result) {
                _this.orders.unshift(newOrder);
                _this.selected = '3'; // 切换到待付款页面
            }
        },
        pay: function () {
            this.showToast('支付成功', 'success', 1000);
        },
        // 拷贝订单对象
        deepCopy: function(source) {
            var result={};
            for (var key in source) {
                result[key] = typeof source[key]==='object'? this.deepCopy(source[key]): source[key];
            }
            return result;
        }
    },
    mounted: function () { // 页面初始化事件 类似与window.onload
        this.getOrders();
        // 根据参数 展示不同的tab
        console.log(window.location.search.substr(-1));
        this.selected = window.location.search.substr(-1);
    },
    mixins: [SystemMixin]
});